package com.me.util;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ImageUtil {

	
	public static void resizeImgToJpeg(InputStream file,int width,int height,String filename) throws IOException{
		BufferedImage bImage=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Image img=ImageIO.read(file);
		bImage.getGraphics().drawImage(img, 0, 0, width,height,null);
		File destFile=new File(filename);
		FileOutputStream out=new FileOutputStream(destFile);
		JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);
		
		encoder.encode(bImage);
		
		out.close();
	}
	
	public void currImgType(){
		
	}
	
	public static void createFolder(String folderName){
		File file=new File(folderName);
		file.mkdirs();
	}
	
	
	
	public static boolean isImage(InputStream is){
		boolean flag=true;
		try {
			Image img=ImageIO.read(is);
			if(img==null){
				flag=false;
			}else{
				flag=true;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("读取文件失败");
			flag=false;
			e.printStackTrace();
		}
		
		
		
		return flag;
	}
	
	
	public static void saveOriFile(String filename,InputStream is) throws IOException{
		File file=new File(filename);
		
		FileOutputStream fos=new FileOutputStream(file);
		byte[] bytes=new byte[1024];
		while((is.read(bytes))!=-1){
			fos.write(bytes);
		}
		is.close();
		fos.close();
		
	}
	
	public static String getPrefix(String filename){
		return filename.substring(filename.lastIndexOf(".")+1);
	}
	
	
}
